package org.syncsoft.overalt.communication.impl;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.syncsoft.overalt.common.api.Parameter;
import org.syncsoft.overalt.communication.exception.ConnectionException;

/**
 * represents the connected status of a connection
 * 
 * @author tptfc
 *
 */
public class ConnectedConnectionStatus extends AbstractConnectionStatus {
	public ConnectedConnectionStatus(DefaultHttpClient httpClient) {
		super(httpClient);
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionStatus#open()
	 */
	@Override
	public void open() {
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionStatus#close()
	 */
	@Override
	public void close() {
		if (getHttpClient() != null) {
			getHttpClient().getConnectionManager().shutdown();
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionStatus#get(java.lang.String, java.util.Map)
	 */
	@Override
	public HttpResponse get(String uri, List<Parameter> parameters) throws ConnectionException {
		if (uri.contains("?")) {
			uri += "&";
		} else {
			uri += "?";
		}
		
		HttpGet httpGet = new HttpGet(uri + buildGetRequestParameters(parameters));
		return executeRequest(httpGet);
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionStatus#post(java.lang.String, java.util.Map)
	 */
	@Override
	public HttpResponse post(String uri, List<Parameter> parameters) throws ConnectionException {
		HttpPost httpPost = new HttpPost(uri);

		try {
			httpPost.setEntity(buildPostRequestParameters(parameters));
		} catch (UnsupportedEncodingException e) {
			throw new ConnectionException(e);
		}
		
		return executeRequest(httpPost);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.communication.api.IConnectionStatus#postMultipart(java.lang.String, java.util.List, java.lang.String, java.io.File)
	 */
	@Override
	public HttpResponse postMultipart(String uri, List<Parameter> parameters, String fileEntityName, File file) throws ConnectionException {
		HttpPost httpPost = new HttpPost(uri);
		
		try {
			MultipartEntity entity = buildPostMultipartRequestParameters(parameters, fileEntityName, file);
			httpPost.setEntity(entity);
		} catch (UnsupportedEncodingException e) {
			throw new ConnectionException(e);
		}
		
		               
		return executeRequest(httpPost);
	}
	
	/**
	 * @precondition requestBase != null
	 * @postCondition request to server
	 * 
	 * @param requestBase
	 * @return null if there is problems
	 * @throws ConnectionException 
	 */
	private HttpResponse executeRequest(HttpRequestBase requestBase) throws ConnectionException {
		HttpResponse response = null;
		
		try {
			response = getHttpClient().execute(requestBase);
		} catch (Exception e) {
			throw new ConnectionException(e);
		}
		
		return response;
	}
}
